package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisDemoSet {
    // sadd 和 sismember
    public static void test01(Jedis jedis) {
        System.out.println("sadd 和 sismember");
        jedis.flushAll();
        System.out.println(jedis.sadd("key1", "111", "222", "333", "444"));
        System.out.println(jedis.sismember("key1", "111"));
        System.out.println(jedis.sismember("key1", "1"));
        System.out.println(jedis.smembers("key1"));
    }

    // scard、spop、smove 和 srem
    public static void test02(Jedis jedis) {
        System.out.println("scard: " + jedis.scard("key1"));
        System.out.println("spop: " + jedis.spop("key1"));
        jedis.sadd("key2", "1", "2", "3");
        System.out.println(jedis.smove("key2", "key3", "1"));
        System.out.println(jedis.smembers("key3"));
        System.out.println("srem: " + jedis.srem("key2", "1", "2"));
        System.out.println(jedis.smembers("key2"));
    }

    // 交集、并集 和 差集
    public static void test03(Jedis jedis) {
        jedis.flushAll();
        jedis.sadd("key1", "1", "2", "3", "4");
        jedis.sadd("key2", "2", "4", "6", "8");
        System.out.println("key1 和 key2 的交集: " + jedis.sinter("key1", "key2"));
        System.out.println("key1 和 key2 的并集: " + jedis.sunion("key1", "key2"));
        System.out.println("key1 和 key2 的差集: " + jedis.sdiff("key1", "key2"));
        jedis.sinterstore("key3", "key1", "key2");
        System.out.println(jedis.smembers("key3"));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool(Constant.IP);
        try (Jedis jedis = jedisPool.getResource()){
//            test01(jedis);
//            test02(jedis);
            test03(jedis);
        }
    }
}
